package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.DeviceCategoryMapper;
import com.yunhai.device.mapper.DeviceMaintainMapper;
import com.yunhai.device.service.DeviceMaintainService;
import com.yunhai.model.entity.dms.DmsDeviceCategoryEntity;
import com.yunhai.model.entity.dms.DmsDeviceMaintainEntity;
import com.yunhai.model.vo.dms.DmsDeviceMaintainQuery;
import com.yunhai.model.vo.dms.DmsDeviceMaintainVo;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class DeviceMaintainServiceImpl implements DeviceMaintainService {

    @Resource
    private DeviceMaintainMapper deviceMaintainMapper;
    @Resource
    private DeviceCategoryMapper categoryMapper;

    /**
     * 获取 DeviceMaintain 分页列表
     */
    @Transactional
    public IPage getDeviceMaintainPagination(PaginationVo paginationVo, DmsDeviceMaintainQuery dmsDeviceMaintainQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsDeviceMaintainEntity> lqs = new LambdaQueryWrapper();

        lqs.eq(dmsDeviceMaintainQuery.getCategoryId()!=null,
                DmsDeviceMaintainEntity::getCategoryId,
                dmsDeviceMaintainQuery.getCategoryId()
                );
        lqs.orderByDesc( DmsDeviceMaintainEntity::getCreateTime );
        deviceMaintainMapper.selectPage(page, lqs);

        List<DmsDeviceMaintainEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        Set<Long> categoryIds = record.stream().map(item->item.getCategoryId()).collect(Collectors.toSet());
        if( !ObjectUtil.isNotEmptyList(categoryIds) ){
            return page;
        }

        List<DmsDeviceCategoryEntity> categoryList = categoryMapper.selectBatchIds(categoryIds);
        if( !ObjectUtil.isNotEmptyList(categoryList) ){
            return page;
        }

        for( DmsDeviceMaintainEntity detail : record ){
            categoryList.stream().filter(row->{
                return row.getId().equals(detail.getCategoryId());
            }).findFirst().ifPresent( row->{
                detail.setCategoryName(row.getName());
            });
        }

        return page;
    }


    /**
     * 获取 DeviceMaintain 所有数据
     *
     */
    @Transactional
    public List<DmsDeviceMaintainEntity> getDeviceMaintainAllList( DmsDeviceMaintainQuery dmsDeviceMaintainQuery ) throws Exception {

        LambdaQueryWrapper<DmsDeviceMaintainEntity> lqs = new LambdaQueryWrapper();
        List<DmsDeviceMaintainEntity> list = deviceMaintainMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 DeviceMaintain 详情
     */
    @Transactional
    public DmsDeviceMaintainEntity getDeviceMaintainDetail( Long id) throws Exception {

        DmsDeviceMaintainEntity dmsDeviceMaintainEntity = deviceMaintainMapper.selectById(id);
        return dmsDeviceMaintainEntity;
    }


    /**
     * 新增 DeviceMaintain
     * */
    @Transactional
    public void addDeviceMaintain(DmsDeviceMaintainVo dmsDeviceMaintainVo) throws Exception {

        DmsDeviceMaintainEntity dmsDeviceMaintainEntity = new DmsDeviceMaintainEntity();
        ReflectionCopy.copyProperties( dmsDeviceMaintainEntity, dmsDeviceMaintainVo );
        deviceMaintainMapper.insert(dmsDeviceMaintainEntity);
        return;
    }


    /**
     * 更新 DeviceMaintain
     * */
    @Transactional
    public void updateDeviceMaintain( DmsDeviceMaintainVo dmsDeviceMaintainVo) throws Exception {

        if( dmsDeviceMaintainVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsDeviceMaintainEntity dmsDeviceMaintainEntity = new DmsDeviceMaintainEntity();
        ReflectionCopy.copyProperties( dmsDeviceMaintainEntity, dmsDeviceMaintainVo );
        deviceMaintainMapper.updateById(dmsDeviceMaintainEntity);
        return;
    }

    /**
     * 删除 DeviceMaintain
     * */
    @Transactional
    public void batchRemoveDeviceMaintain( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        deviceMaintainMapper.deleteBatchIds(ids);
    }

}